package com.cloudbroker.bcs.platform.dav.configure.define.parser.formfield;

import org.dom4j.Element;

import com.cloudbroker.bcs.platform.dav.configure.define.element.FormField;
import com.cloudbroker.bcs.platform.dav.configure.define.element.HTMLFragment;
import com.cloudbroker.bcs.platform.dav.configure.define.element.Property;
import com.cloudbroker.bcs.platform.dav.configure.define.parser.ConfigureParser;

public abstract class SingleValidatorFormFieldParser<G extends FormField<?, ?>> extends BaseFormFieldParser<G> {
    
    public SingleValidatorFormFieldParser(ConfigureParser configureParser) {
        super(configureParser);
    }
    
    @Override
    public G parseHTMLFragment(Element element, HTMLFragment<?, ?> contextFragment) {
        if (contextFragment instanceof Property) {
            if (((Property) contextFragment).getFragments().size() > 1) {
                throw new IllegalArgumentException(
                        parseFor()
                                + " validator(select/radio/checkbox) should appear first and no more than once in one property");
            }
        }
        
        G formFiled = createFormField();
        G parsedFormFiled = parseBasicInfoAndDealDefine(element, formFiled, contextFragment);
        
        configureParser.parseChildElements(parsedFormFiled, element);
        
        return parsedFormFiled;
    }
    
    protected abstract G createFormField();
    
}
